#!/bin/sh
#Barry Kauler 2006 www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v406 support for old kernel /dev/hd*.
#v407 fix for floppy drive icon.
#v409 gparted create ext3 part. failed, fixed by making /etc/mtab a symlink.
#v423 recent version busybox umount, default for umount is to not free loop device, needs -d.
#120104 a bug if more than 9 partitions in drive, desktop icons may display incorrect mounted status.
#130203 change probedisk2 to probedisk.


[ ! $1 ] && exec busybox umount

#sync #paranoid precaution. v2.12 kernel call to modprobe, sync hangs.

. /etc/rc.d/functions4puppy4 #v4.02

#v406 add /dev/hd* support... v407 add fd*...
MNTDDEVS1="`busybox mount | grep '^/dev/[smhf]' | cut -f 1 -d ' ' | cut -f 3 -d '/'`" #v403 sd, sr, mmc

#extract all the '-' options, on separate lines...
#v2.12 pathetic bug, need space before $@, see comment /bin/mount... No, use $*...
DASHOPTS="`echo "$*" | tr '\t' ' ' | tr ' ' '\n' | grep '^\-'`"

#v2.12 curses, busybox mount accepts the '-n' option (just ignores it) but umount does not...
if [ ! "`echo " $DASHOPTS" | grep 'n'`" = "" ];then
 FULLCMD="`echo -n " $*" | sed -e 's/ \-n / /g'`" #unsatisfactory!
else
 FULLCMD="$*"
fi

# -a param means unmount everything...
#if [ ! "`echo "$@" | grep --extended-regexp '\W-a$|\W-a\W'`" = "" ];then
if [ ! "`echo " $DASHOPTS" | grep 'a'`" = "" ];then
 MNTFUSE="`busybox mount | grep 'fuse' | head -n 1 | cut -f 3 -d ' '`"
 [ ! "$MNTFUSE" = "" ] && fusermount -u $MNTFUSE
 busybox umount -d $FULLCMD #v423 -d fix.
 exit $?
fi

#remove everything except the device or mntpt...
LASTPARAM="`echo -n "$*" | tr '\t' ' ' | tr -s ' ' | tr ' ' '\n' | grep '^/'`"

#thanks to jesse for this...
#for ntfs-3g, find the device and mount point...
#note, '\' essential to prevent 'ps' from reporting this invocation line...
NTFSMNTPT="`ps -e | grep -o 'ntfs\-3g.*' | grep "$LASTPARAM" | tr '\t' ' ' | tr -s ' ' | tr ' ' "\n" | grep '^/mnt/'`"

if [ "$NTFSMNTPT" = "" ];then
 busybox umount -d $FULLCMD #v423 -d fix to free loop device.
 RETVAL=$?
else
 #fusermount can only unmount by giving the mount-point...
 fusermount -u $NTFSMNTPT
 RETVAL=$?
fi

#v403 if there is a desktop icon (see pup_eventd), then refresh it...
if [ $RETVAL -eq 0 ];then
 #find which device has been unmounted... v406 add hd*... v407 add fd*...
 MNTDDEVS2="`busybox mount | grep '^/dev/[smhf]' | cut -f 1 -d ' ' | cut -f 3 -d '/'`" #v403 sd, sr, mmc
 if [ "$MNTDDEVS2" = "" ];then
  DEVNAME="`echo "$MNTDDEVS1" | head -n 1`" #head is a precaution.
 else
  DEVNAME="`echo "${MNTDDEVS1}
${MNTDDEVS2}" | sort | uniq -u | head -n 1`" #head is a precaution.
 fi
 if [ "$DEVNAME" != "" -a "$DISPLAY" != "" ];then
  DRVNAME="`echo -n "$DEVNAME" | cut -b 1-3`" #ex: DRVNAME=sda
  #special case, SD card /dev/mmcblk0p1...
  [ "$DRVNAME" = "mmc" ] && DRVNAME="`echo -n "$DEVNAME" | sed -e 's/p[0-9]$//'`"
  xDRVNAME="$DRVNAME" #v404
  [ -d /root/.pup_event/drive_${DEVNAME} ] && DRVNAME="$DEVNAME" #icon for each partition. ex: DRVNAME=sda1
  if [ -d /root/.pup_event/drive_${DRVNAME} ];then
   case $DRVNAME in #v407
    fd*)
     DRV_CATEGORY="floppy"
    ;;
    *)
     dnPATTERN='/dev/'"${xDRVNAME}"'|'
     DRV_CATEGORY="`probedisk | grep "$dnPATTERN" | cut -f 2 -d '|'`"
    ;;
   esac
   #if any partitions still mounted, leave "MNTD" on icon...
   drvPATTERN='^/dev/'"${DRVNAME}" #important, no space on end.
   #120104 a bug if more than 9 partitions in drive, ex DRVNAME=sda1 xDRVNAME=sda, df test below must ignore sda10 etc...
   [ "$DRVNAME" != "$xDRVNAME" ] && drvPATTERN='^/dev/'"${DRVNAME}"' ' #120104 we do want a space on end.
   #"MNTD" text is intended as a reminder to user that drive needs to be unmounted,
   #   but partitions in use by Puppy cannot be unmounted.
   if [ "`df | tr -s ' ' | cut -f 1,6 -d ' ' | grep "$drvPATTERN" | grep -v ' /initrd/' | grep -v ' /$'`" = "" ];then
    if [ "`df | tr -s ' ' | cut -f 1,6 -d ' ' | grep "$drvPATTERN" | grep -E ' /initrd/| /$'`" != "" ];then
     #only a partition left mntd that is in use by puppy, change green->yellow...
     icon_mounted_func $DRVNAME $DRV_CATEGORY #see functions4puppy4
    else
     #redraw icon without "MNTD" text...
     icon_unmounted_func $DRVNAME $DRV_CATEGORY #see functions4puppy4
    fi
   fi
  fi
 fi
fi

#v409, instead just make sure the symlink stays there...
if [ ! -L /etc/mtab ];then
 rm -f /etc/mtab
 ln -s /proc/mounts /etc/mtab
fi

exit $RETVAL
